package com.onec.service.auth.entity;


import com.fasterxml.jackson.annotation.JsonFormat;
import com.onec.service.api.content.AesEntityContent;
import com.onec.service.api.enums.StatusEnum;
import lombok.Data;
import org.hibernate.annotations.ColumnTransformer;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import javax.persistence.*;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;

/**
 * @author ONEC
 */
@Table(name = "t_customer_info")
@Entity
@Data
public class CustomerInfo implements UserDetails, Serializable {
    /**
     * id
     */
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id", unique = true)
    private Long id;


    /**
     * 用户手机号
     */
    @Column(name = "username", length = 128)
    @ColumnTransformer(
            read = AesEntityContent.AES_DECRYPT_PHONE,
            write = AesEntityContent.AES_ENCRYPT_PHONE
    )
    private String username;
    /**
     * 版本控制
     */
    @Column(name = "version")
    private Integer version;

    @Column(name = "client_id")
    private String clientId;

    /**
     * 用户编号
     */
    @Column(name = "customer_no")
    private String customerNo;

    /**
     * 用户手机号
     */
    @Column(name = "phone", length = 128)
    @ColumnTransformer(
            read = AesEntityContent.AES_DECRYPT_PHONE,
            write = AesEntityContent.AES_ENCRYPT_PHONE
    )
    private String phone;
    /**
     * 用户手机号
     */
    @Column(name = "merchant_id", length = 12)
    private Long merchantId;

    /**
     * 用户邮箱
     */
    @Column(name = "email", length = 128)
    private String email;

    /**
     * 用户密码
     */
    @Column(name = "password", length = 256)
    private String password;

    /**
     * 用户状态
     */
    @Column(name = "status", length = 12)
    @Enumerated(EnumType.STRING)
    private StatusEnum status;

    /**
     * 创建日期
     */
    @Column(name = "create_time", length = 0)
    @Temporal(TemporalType.TIMESTAMP)
    @CreatedDate
    @JsonFormat(pattern = "yyyy-MM-dd HH:ss:mm", timezone = "GTM+8")
    private Date createTime;

    /**
     * 变更日期
     */
    @Column(name = "update_time", length = 0)
    @LastModifiedDate
    @Temporal(TemporalType.TIMESTAMP)
    @JsonFormat(pattern = "yyyy-MM-dd HH:ss:mm", timezone = "GTM+8")
    private Date updateTime;

    /**
     * 备注信息
     */
    @Column(name = "remark", length = 256)
    private String remark;

    private static final long serialVersionUID = 1L;


    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        Collection<GrantedAuthority> authorities = new ArrayList<>();
        String username = this.getUsername();
        if (username != null) {
            SimpleGrantedAuthority authority = new SimpleGrantedAuthority(username);
            authorities.add(authority);
        }
        return authorities;
    }


    @Override
    public String getUsername() {
        return this.phone;
    }

    @Override
    public boolean isAccountNonExpired() {
        return false;
    }

    @Override
    public boolean isAccountNonLocked() {
        return false;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return false;
    }

    @Override
    public boolean isEnabled() {
        return false;
    }

}
